package me.zhengjie.modules.website.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import me.zhengjie.modules.website.domain.CmInfos;
import me.zhengjie.modules.website.service.CmInfosService;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import me.zhengjie.modules.website.domain.PageResult;
import java.util.List;

/**
 *
 *
 * @author system
 * @since 2025-01-14 17:56:04
 */
@RestController
@RequestMapping("/api/cmInfos")
@Api(tags = " 控制器")
public class CmInfosController {

    @Autowired
    private CmInfosService cmInfosService;

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "分页查询",notes = "分页查询",produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/findPage")
    public ResponseEntity<PageResult<CmInfos>> findPage(@RequestBody CmInfos params, Pageable pageable) {
        PageResult<CmInfos> result = cmInfosService.findPage(params, pageable);
        return ResponseEntity.ok(result);
    }
    /**
     * 列表查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "列表查询",notes = "列表查询",produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/findList")
    public ResponseEntity<List<CmInfos>> findList(@RequestBody CmInfos params) {
        List<CmInfos> result = cmInfosService.findList(params);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping("/{id}")
    public ResponseEntity<CmInfos> findById(@PathVariable("id") Long id) {
        CmInfos cmInfos = cmInfosService.findById(id);
        return ResponseEntity.ok(cmInfos);
    }

    /**
     * 新增
     *
     * @param cmInfos
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PostMapping
    public ResponseEntity<CmInfos> insert( @Validated  @RequestBody CmInfos cmInfos) {
        CmInfos result = cmInfosService.insert(cmInfos);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改
     *
     * @param cmInfos
     * @return
     */
    @ApiOperation(value = "修改", notes = "修改数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PutMapping
    public ResponseEntity<CmInfos> update( @Validated  @RequestBody CmInfos cmInfos) {
        CmInfos result = cmInfosService.update(cmInfos);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除数据")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        cmInfosService.delete(id);
        return ResponseEntity.ok(null);
    }

    @ApiOperation(value = "分页条件查询CM信息", notes = "根据infoTitle、infoType模糊匹配并分页")
    @GetMapping("/search")
    public ResponseEntity<Page<CmInfos>> search(
            @RequestParam(required = false) String infoTitle,
            @RequestParam(required = false) String infoType,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<CmInfos> result = cmInfosService.search(infoTitle, infoType, pageRequest);
        return ResponseEntity.ok(result);
    }

}